package hu.kisprof.quiz.view.panels;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.TreeSet;

import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

public class QuestionsListPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4295131070058455517L;
	private QuizGUI gui;
	private JScrollPane questionsScrollPane;
	
	private JList questionsList;
	
	
	public QuestionsListPanel(QuizGUI gui){
		super(new BorderLayout());
		this.gui=gui;
		
		refresh();
	}


	public void refresh() {
		this.removeAll();
		JLabel questionLabel = new JLabel(Labels.questions);
		questionLabel.setFont(new Font(questionLabel.getFont().getFontName(),Font.BOLD,24));
		this.add(questionLabel, BorderLayout.NORTH);
		
		
		TreeSet<String> questions =gui.getControl().getQuestions();
		DefaultListModel questionsListModel = new DefaultListModel();
		Iterator<String> it = questions.iterator();
		
		while(it.hasNext()){
			questionsListModel.addElement(it.next());
		}
		
		if(questionsList != null){
			JList tmpQuestionsList = new JList(questionsListModel);
			
			ListSelectionListener[] lsls = questionsList.getListSelectionListeners();
			for (int i=0;i<lsls.length;i++){
				tmpQuestionsList.addListSelectionListener(lsls[i]);
			}
			MouseListener[] mls = questionsList.getMouseListeners();
			for(int i=0; i<mls.length;i++){
				tmpQuestionsList.addMouseListener(mls[i]);
			}
			
			questionsList =tmpQuestionsList;
		} else{
			questionsList = new JList(questionsListModel);
		}
			questionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			
			questionsScrollPane = new JScrollPane(questionsList);
			this.add(questionsScrollPane, BorderLayout.CENTER);
			this.validate();
	}
	public JList getQuestionsList(){
		return questionsList;
	}
}
